package com.xbongbong.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 定义动态数据源
 * @Author 梁鲁江
 * 创建时间 2021/10/13 15:02
 * 修改时间  2021/10/13 15:02
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
    private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
 
    /**
     * 配置DataSource, defaultTargetDataSource为主数据库
     */
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }
 
    @Override
    protected Object determineCurrentLookupKey() {
        return getDataSource();
    }
 
    public static void setDataSource(String dataSource) {
        CONTEXT_HOLDER.set(dataSource);
    }
 
    public static String getDataSource() {
        return CONTEXT_HOLDER.get();
    }
 
    public static void clearDataSource() {
        CONTEXT_HOLDER.remove();
    }
 
}
